<template>
  <div class="app-container">

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name="qb" />
      <el-tab-pane v-for="item in langList" :key="item.id" :label="item.langCn" />
    </el-tabs>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="语言" prop="langId">
        <el-select v-model="queryParams.langId" placeholder="请选择" @change="getList">
          <el-option
            v-for="item in langList"
            :key="item.id"
            :label="item.langCn"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分销商" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分销商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="价格" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入价格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="原价" prop="originalPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.originalPrice"-->
<!--          placeholder="请输入原价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="等级" prop="level">-->
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="请输入等级"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="直接分润" prop="direct">-->
<!--        <el-input-->
<!--          v-model="queryParams.direct"-->
<!--          placeholder="请输入直接分润"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="间接分润" prop="indirect">-->
<!--        <el-input-->
<!--          v-model="queryParams.indirect"-->
<!--          placeholder="请输入间接分润"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期" prop="expire">-->
<!--        <el-input-->
<!--          v-model="queryParams.expire"-->
<!--          placeholder="请输入有效期"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序" prop="weigh">-->
<!--        <el-input-->
<!--          v-model="queryParams.weigh"-->
<!--          placeholder="请输入排序"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatetime"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="删除时间" prop="deletetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.deletetime"-->
<!--          placeholder="请输入删除时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" style="text-align: right">
        <el-button
          icon="el-icon-delete"
          size="mini"
          @click="handleRecycle"
        >回收站</el-button>
      </el-col>
      <el-col :span="18" style="text-align: right">
        <el-button
          icon="el-icon-search"
          size="mini"
          @click="handleShow"
        > </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="resellerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="${comment}" align="center" prop="siteId" />-->
      <el-table-column label="语言" align="center" prop="vsLangInfo.langCn" />
      <el-table-column label="分销商" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="介绍" align="center" prop="content" />-->
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="原价" align="center" prop="originalPrice" />
<!--      <el-table-column label="等级" align="center" prop="level" />-->
      <el-table-column label="直接分润（%）" align="center" prop="direct" />
      <el-table-column label="间接分润（%）" align="center" prop="indirect" />
      <el-table-column label="有效期" align="center" prop="expire" />
      <el-table-column label="排序" align="center" prop="weigh" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status == 'normal'">显示</span>
          <span v-if="scope.row.status == 'hidden'">隐藏</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="更新时间" align="center" prop="updatetime" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createtime" />-->
<!--      <el-table-column label="删除时间" align="center" prop="deletetime" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分销商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="语言" prop="langId">
          <el-select v-model="form.langId" placeholder="请选择">
            <el-option
              v-for="item in langList"
              :key="item.id"
              :label="item.langCn"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分销商" prop="name">
          <el-input v-model="form.name" placeholder="请输入分销商" />
        </el-form-item>
<!--        <el-form-item label="图片" prop="image">-->
<!--          <image-upload v-model="form.image"/>-->
<!--        </el-form-item>-->
        <el-form-item label="介绍">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入等级" />
        </el-form-item>
        <el-form-item label="直接分润" prop="direct">
          <el-input v-model="form.direct" placeholder="请输入直接分润" />
        </el-form-item>
        <el-form-item label="间接分润" prop="indirect">
          <el-input v-model="form.indirect" placeholder="请输入间接分润" />
        </el-form-item>
        <el-form-item label="有效期" prop="expire">
          <el-input v-model="form.expire" placeholder="请输入有效期" />
        </el-form-item>
        <el-form-item label="排序" prop="weigh">
          <el-input v-model="form.weigh" placeholder="请输入排序" />
        </el-form-item>
<!--        <el-form-item label="删除时间" prop="deletetime">-->
<!--          <el-input v-model="form.deletetime" placeholder="请输入删除时间" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 回收站管理 -->
    <el-dialog title="回收站" :visible.sync="openDel" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            size="mini"
            :disabled="single"
            @click="handleReduction"
          >还原</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="single"
            @click="handleDestruction"
          >销毁</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-refresh-right"
            size="mini"
            @click="batchReduction"
          >还原全部</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="batchDestruction"
          >清空回收站</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="delLoading" :data="delList" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="删除时间" align="center" prop="deletetime" />
        <el-table-column fixed="right" width="150px" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="clickReduction(scope.row)"
            >还原</el-button>
            <el-button
              size="mini"
              type="text"
              @click="clickDestruction(scope.row)"
            >销毁</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="delTotal>0"
        :total="delTotal"
        :page.sync="delParams.pageNum"
        :limit.sync="delParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listReseller, getReseller, delReseller, addReseller, updateReseller } from "@/api/reseller/reseller";
import {listLang} from "@/api/video/lang";

export default {
  name: "Reseller",
  data() {
    return {
      activeName: 'qb',
      langList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 分销商表格数据
      resellerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        langId: '',
        name: null,
        image: null,
        content: null,
        price: null,
        originalPrice: null,
        level: null,
        direct: null,
        indirect: null,
        expire: null,
        weigh: null,
        status: null,
        updatetime: null,
        createtime: null,
        deletetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        langId: [
          { required: true, message: "语言不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "分销商不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "介绍不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "等级不能为空", trigger: "blur" }
        ],
        direct: [
          { required: true, message: "直接分润不能为空", trigger: "blur" }
        ],
        indirect: [
          { required: true, message: "间接分润不能为空", trigger: "blur" }
        ],
        expire: [
          { required: true, message: "有效期不能为空", trigger: "blur" }
        ],
        weigh: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态:normal=显示,hidden=隐藏不能为空", trigger: "change" }
        ],
      },
      openDel: false,
      delList: [],
      delLoading: false,
      delTotal: 0,
      delParams: {
        pageNum: 1,
        pageSize: 10,
        deletetime: 1
      },
    };
  },
  created() {
    this.getLangList();
  },
  methods: {
    /** 查询语言包列表 */
    getLangList() {
      this.loading = true;
      listLang().then(response => {
        this.langList = response.rows;
        this.handleClick();
        this.loading = false;
      });
    },
    handleClick() {
      if(this.activeName == 'qb') {
        this.queryParams.langId = ''
      } else {
        this.queryParams.langId = this.langList[this.activeName-1].id;
      }
      this.getList();
    },
    /** 查询分销商列表 */
    getList() {
      this.loading = true;
      listReseller(this.queryParams).then(response => {
        this.resellerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        langId: null,
        name: null,
        image: null,
        content: null,
        price: null,
        originalPrice: null,
        level: null,
        direct: null,
        indirect: null,
        expire: null,
        weigh: null,
        status: null,
        updatetime: null,
        createtime: null,
        deletetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分销商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReseller(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分销商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReseller(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReseller(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      row.deletetime = new Date().getTime()
      updateReseller(row).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.open = false;
        this.getList();
      }).catch(() => {});
    },
    handleShow() {
      this.showSearch = !this.showSearch;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reseller/export', {
        ...this.queryParams
      }, `reseller_${new Date().getTime()}.xlsx`)
    },
    /** 回收站dialog */
    handleRecycle() {
      this.openDel = true;
      this.getDelList();
    },
    /** 查询回收站VIP订单列表 */
    getDelList() {
      this.delLoading = true;
      listReseller(this.delParams).then(response => {
        this.delList = response.rows;
        this.delTotal = response.total;
        this.delLoading = false;
      });
    },
    /** 多选还原数据 */
    handleReduction() {
      let num = 0;
      this.ids.forEach(id => {
        let params = {
          id: id,
          deletetime: null
        }
        updateReseller(params).then(response => {
        }).catch(e => {
          num++;
        });
      })
      if(num == 0) {
        this.$modal.msgSuccess("还原成功");
        this.getDelList();
      }
    },
    /** 多选销毁数据 */
    handleDestruction() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除短剧编号为"' + ids + '"的数据项？').then(function() {
        return delReseller(ids);
      }).then(() => {
        this.getDelList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 还原全部数据 */
    batchReduction() {
      if(this.delList.length > 0) {
        let num = 0;
        this.delList.forEach(item => {
          item.deletetime = null
          updateReseller(item).then(response => {
          }).catch(e => {
            num++;
          });
        })
        if(num == 0) {
          this.$modal.msgSuccess("还原成功");
          this.getDelList();
        }
      } else {
        this.$modal.msgWarning("回收站暂无数据");
      }
    },
    /** 销毁全部数据 */
    batchDestruction() {
      if(this.delList.length > 0) {
        const ids = this.delList.map(obj => obj.id);
        this.$modal.confirm('是否确认删除回收站中的全部数据？').then(function() {
          return delReseller(ids);
        }).then(() => {
          this.getDelList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      } else {
        this.$modal.msgWarning("回收站已经是空的了");
      }
    },
    /** 行间单击还原数据 */
    clickReduction(row) {
      row.deletetime = null
      updateReseller(row).then(response => {
        this.$modal.msgSuccess("还原成功");
        this.getDelList()
      });
    },
    /** 行间单击销毁数据 */
    clickDestruction(row) {
      const ids = [];
      ids.push(row.id)
      this.$modal.confirm('是否确认删除短剧编号为"' + ids + '"的数据项？').then(function() {
        return delReseller(ids);
      }).then(() => {
        this.getDelList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
